'use strict'
import axios from 'axios'
import {Message} from 'element-ui';


export let appHeaders = {
    authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIzM2I5MWFiMC1iMDNlLTQ3NzAtOGJiNS0zOGJhZTE5ZDU5YjkiLCJzdWIiOiJXZWIgQVBJIFBhc3Nwb3J0IiwiYXBwX2lkIjoiMzNiOTFhYjAtYjAzZS00NzcwLThiYjUtMzhiYWUxOWQ1OWI5IiwiYXBwX2NvZGUiOiJGcDBoTndJVUY2QTF0VTNUIiwiZW5kX3VzZXJfaWQiOiI4MjhjMzNiOS0zMmIzLTQwZWMtOTIzZS1hZmE1ZThlNzA3MTAifQ.JWy4t_q6qpBBVc_NKLm_GnwPryZKubRss4vQ5YaQukI",
    baggage: "sentry-environment=production,sentry-public_key=c0bcc0e36783694f41e4fb1e6a3efea9,sentry-trace_id=da13a838bc234eb980f87322e1308950,sentry-sample_rate=0.1,sentry-sampled=false",
}

let config = {
    baseURL: process.env.NODE_ENV === 'production' ? '/api' : '/api',
    timeout: 60 * 1000, // Timeout
    withCredentials: true // Check cross-site Access-Control
}

let cancel,
    promiseArr = {}

let CachePageSize = {}

const _axios = axios.create(config)

_axios.interceptors.request.use(
    function (config) {
        config.params = {...config.params}

        // Do something before request is sent
        if (promiseArr[config.url]) {
            promiseArr[config.url]('操作取消')
            promiseArr[config.url] = cancel
        } else {
            promiseArr[config.url] = cancel
        }

        const urlKey = config.method + config.url
        const pageSize = config.params.size
        if (pageSize) {
            CachePageSize[urlKey] = pageSize
        } else if (CachePageSize[urlKey]) {
            config.params.size = CachePageSize[urlKey]
        }
        Object.assign(config.headers, appHeaders)

        return config
    },
    function (error) {
        // Do something with request error
        return Promise.reject(error)
    }
)

// Add a response interceptor
_axios.interceptors.response.use(
    function (response) {
        const dataAxios = response.data
        const {retCode, data, retMsg} = dataAxios
        if (retCode === undefined) {
            return dataAxios
        } else {
            switch (retCode) {
                case 1:
                    retMsg && Message.success(retMsg)
                    return dataAxios
                case 200:
                    retMsg && Message.success(retMsg)
                    return dataAxios
                default:
                    // retMsg && Notify({type: 'warning', message: retMsg})
                    retMsg && Message.error(retMsg)
                    return Promise.reject(dataAxios)
            }
        }
    },
    function (error) {
        // Do something with response error
        if (error && error.response) {
            const response = error.response
            const status = response.status

            if (status === 401) {
                return Promise.reject(error)
            }
            if (status === 403) {
                return Promise.reject(error)
            }

            Message.error(status || response.statusText)
        } else {
            console.error(error)
        }
        return Promise.reject(error)
    }
)

export default _axios
